<template>
  <div class="tenantMenu_main main_container">
    <div class="main_header">
      <div class="padding_container">
        <div class="left"><Breadcrumb /></div>
        <div class="right">
          <!--          <a-->
          <!--            v-hasPermiHide="['engineering:require:processArrange']"-->
          <!--            class="button_item"-->
          <!--            @click.stop="superPlanning"-->
          <!--          >-->
          <!--            <div class="button_item_padding">-->
          <!--              <svg-icon icon-class="工艺编排" />-->
          <!--              超级编排-->
          <!--            </div>-->
          <!--          </a>-->
          <a
            v-hasPermiHide="['engineering:require:processArrange']"
            class="button_item"
            @click.stop="superPlanning"
          >
            <div class="button_item_padding">
              <svg-icon icon-class="工艺编排" />
              工艺编排
            </div>
          </a>
          <HeaderOperation :parent="this" />
        </div>
      </div>
    </div>
    <div class="main_main">
      <SearchBar :parent="this" />
      <div ref="table_container" class="table_container">
        <el-table
          ref="table"
          :data="tableData"
          stripe
          border
          :height="tableHeight"
          style="width: 99.9%"
          row-key="id"
          :cell-style="cellStyle"
          @cell-click="cellClick"
          @sort-change="(column)=>{sortChange(column, this)}"
          @selection-change="(val)=>{selectionChange(val, this)}"
        >
          <el-table-column type="selection" width="70" align="center" />
          <Column v-for="(column,index) in tableColumns" :key="index" :prop="column.prop" :is-show="column.isShow" :label="column.label" :sortable="column.sortable" :width="column.width" />

          <el-table-column label="操作" align="center" fixed="right" width="340">
            <template slot-scope="scope">
              <el-button
                type="text"
                @click.stop="show(scope.row,false)"
              >预览</el-button>
              <!-- <span v-else class="el-button--text button_grey">预览</span> -->
              <el-button
                v-if="scope.row.status!='1'"
                v-hasPermi="['engineering:require:arrange']"
                type="text"
                @click.stop="show(scope.row,true)"
              >编排</el-button>
              <span v-else class="el-button--text button_grey">编排</span>
              <el-button
                v-if="scope.row.status!='1'"
                v-hasPermi="['engineering:require:edit']"
                type="text"
                @click.stop="edit(scope.row)"
              >编辑</el-button>
              <span v-else class="el-button--text button_grey">编辑</span>
              <el-button
                v-if="scope.row.status!='1' "
                v-hasPermi="['engineering:require:delete']"
                type="text"
                @click.stop="deleteOne(scope.row)"
              >删除</el-button>
              <span v-else class="el-button--text button_grey">删除</span>
              <el-button
                v-if="scope.row.status=='0'"
                v-hasPermi="['engineering:require:copy']"
                type="text"
                @click.stop="copyProcessRequirement(scope.row)"
              >复制</el-button>
              <span v-else class="el-button--text button_grey">复制</span>
              <el-button
                v-if="scope.row.status=='1'"
                type="text"
                @click.stop="reviseEdition(scope.row)"
              >升版</el-button>
              <span v-else class="el-button--text button_grey">升版</span>
            </template>
          </el-table-column>
        </el-table>
      </div>
    </div>
    <div class="main_bottom">
      <pagination
        :total="total"
        :page.sync="pageData.pageNum"
        :limit.sync="pageData.pageSize"
        :selected-num="selectionsAll.length"
        @pagination="getList"
      />
    </div>

  </div>
</template>

<script>
import { list, remove, importMethod } from '@/api/engineeringManage/processRequirementNew'
import AddOrUpdate from './addOrUpdate' // 新增或编辑页面
import ShowDetail from './showDetail'
import { list as materialTypeList } from '@/api/warehouseManagement/materialType'
import ProcessPlanning from '@/views/engineeringManage/processRequirement/processPlanning.vue'
import SuperPlanning from '@/views/engineeringManage/processRequirement/superPlanning.vue'

export default {
  name: 'Index',
  data() {
    return {
      pageId: '工艺要求', // 页面唯一标识
      pageData: {
        pageNum: 1,
        pageSize: 20,
        orderByColumn: 'updateTime',
        isAsc: 'desc'
      },
      total: 1, // 数据一共有多少条
      mainId: 'id', // 表格行ID
      tableHeight: '0', // 表格高度
      tableData: [], // 表格数据
      tableColumns: [], // 表格实际用的字段
      searchColumns: [], // 搜索区实际用的字段
      selections: [], // 表格当前页所选中的行
      selectionsAll: [], // 表格所有页选中的行
      defaultTableColumns: [// 表格常规字段（初始值）
        { prop: 'statusName', label: '状态', canOrder: false, width: '100' },
        { prop: 'code', label: '工艺要求编码', minWidth: '180' },
        { prop: 'materialCode', label: '物料编码', minWidth: '140' },
        { prop: 'materialName', label: '物料名称', canOrder: false, minWidth: '140' },
        { prop: 'materialModel', label: '规格型号', canOrder: false, minWidth: '140' },
        { prop: 'materialTypeName', label: '物料分类', canOrder: false, minWidth: '140' },
        { prop: 'materialUnitName', label: '物料单位', canOrder: false, minWidth: '140' },
        { prop: 'versionCode', label: '版本号', canOrder: false, minWidth: '100' },
        { prop: 'remark', label: '备注', canOrder: false, minWidth: '140' }
      ],
      defaultSearchColumns: [// 搜索区常规字段（初始值）
        { prop: 'status', label: '状态', type: 'select', optionsName: 'statusOptions' },
        { prop: 'materialType', label: '物料分类', type: 'select', optionsName: 'materialTypeOptions' },
        { prop: 'code', label: '工艺要求编码', type: 'input' },
        { prop: 'materialCode', label: '物料编码', type: 'input' },
        { prop: 'materialName', label: '物料名称', type: 'input' },
        { prop: 'materialUnitName', label: '物料单位', type: 'input' },
        { prop: 'materialModel', label: '规格型号', type: 'input' }
      ],
      operationData: [ // 右上角的操作按钮
        { label: '新增', perm: 'engineering:require:add' },
        { label: '导入', perm: 'engineering:require:import' },
        { label: '批量删除', perm: 'engineering:require:delete' },
        { label: '页面配置', perm: '' }
      ],
      materialTypeOptions: [],
      statusOptions: [
        { value: undefined, label: '全部' },
        { value: '0', label: '草稿' },
        { value: '1', label: '已提交' }
      ]
    }
  },
  mounted() {
    // this.$nextTick(() => {
    //   this.tableHeight = this.$refs.table_container.offsetHeight
    // })
    this.getMaterialType()
  },
  methods: {
    cellStyle({ row, column }) {
      if (column.property == 'code') {
        return {
          cursor: 'pointer',
          color: '#387bfc'
        }
      }
    },
    cellClick(row, column, cell) {
      switch (column.property) {
        case 'code':
          this.showDetail(row)
          break
      }
    },
    getList() {
      list(this.pageData).then(res => {
        res.data.forEach((item) => {
          if (item.status == 1) {
            item.canDelete = 0
          }
        })
        this.tableData = res.data
        this.total = res.total
        this.$nextTick(() => {
          this.setSelectRow(this)// 设置行选中
          this.tableHeight = this.$refs.table_container.offsetHeight
        })
      })
    },
    getMaterialType() {
      materialTypeList().then(res => {
        res.data.forEach(item => {
          item['value'] = item.id
          item['label'] = item.name
        })
        this.materialTypeOptions = res.data
      })
    },
    refresh() {
      this.pageData.pageNum = 1
      this.getList()
    },
    add() {
      this.$layer.iframe({// 弹框
        content: {
          content: AddOrUpdate, // 传递的组件对象
          parent: this, // 当前的vue对象
          data: {
            tag: 'add'
          }// props
        },
        area: ['1320px', '850px'],
        title: '新增工艺要求', // 标题
        shadeClose: false // 点击遮罩是否关闭
      })
    },
    edit(row) {
      this.$layer.iframe({// 弹框
        content: {
          content: AddOrUpdate, // 传递的组件对象
          parent: this, // 当前的vue对象
          data: {
            tag: 'edit',
            tagId: row[this.mainId]
          }// props
        },
        area: ['1320px', '850px'],
        title: '编辑工艺要求', // 标题
        shadeClose: false // 点击遮罩是否关闭
      })
    },
    deleteOne(row) {
      const _this = this
      this.$confirm('确定要删除吗', '删除确认', {
        confirmButtonText: '确定',
        cancelButtonText: '取消', cancelButtonClass: 'cancelConfirm',
        type: 'warning'
      }).then(function() {
        remove(row[_this.mainId]).then(res => {
          _this.$message({
            message: res.msg,
            type: 'success'
          })
          _this.getList()
        })
      }).catch(() => {})
    },
    // 当前页的删除接口
    deleteMethod(deleteIds) {
      return remove(deleteIds)
    },
    copyProcessRequirement(row) {
      this.$layer.iframe({// 弹框
        content: {
          content: AddOrUpdate, // 传递的组件对象
          parent: this, // 当前的vue对象
          data: {
            tag: 'copy',
            tagId: row[this.mainId]
          }// props
        },
        area: ['1320px', '850px'],
        title: '复制工艺要求', // 标题
        shadeClose: false // 点击遮罩是否关闭
      })
    },
    reviseEdition(row) {
      this.$layer.iframe({// 弹框
        content: {
          content: AddOrUpdate, // 传递的组件对象
          parent: this, // 当前的vue对象
          data: {
            tag: 'reviseEdition',
            tagId: row[this.mainId]
          }// props
        },
        area: ['1320px', '850px'],
        title: '工艺要求升版', // 标题
        shadeClose: false // 点击遮罩是否关闭
      })
    },
    importMethod(files) { // 导入接口
      importMethod(files).then(res => {
        this.$message({
          message: res.msg,
          type: 'success'
        })
        if (res.code == 200) {
          this.getList()
        }
      })
    },
    showDetail(row) {
      this.$layer.iframe({// 弹框
        content: {
          content: ShowDetail, // 传递的组件对象
          parent: this, // 当前的vue对象
          data: {
            tagId: row[this.mainId]
          }// props
        },
        area: ['1320px', '850px'],
        title: '查看工艺要求', // 标题
        shadeClose: false // 点击遮罩是否关闭
      })
    },
    processPlanning() { // 工艺编排
      this.$layer.iframe({// 弹框
        content: {
          content: ProcessPlanning, // 传递的组件对象
          parent: this, // 当前的vue对象
          data: {
          }// props
        },
        area: ['9999px', '9999px'],
        title: '新增工艺编排', // 标题
        shadeClose: false // 点击遮罩是否关闭
      })
    },
    superPlanning() { // 工艺编排
      this.$layer.iframe({// 弹框
        content: {
          content: SuperPlanning, // 传递的组件对象
          parent: this, // 当前的vue对象
          data: {
            showDetail: true
          }// props
        },
        area: ['9999px', '9999px'],
        title: '工艺编排', // 标题
        shadeClose: false // 点击遮罩是否关闭
      })
    },
    show(row, showDetail) { // 工艺编排
      this.$layer.iframe({// 弹框
        content: {
          content: SuperPlanning, // 传递的组件对象
          parent: this, // 当前的vue对象
          data: {
            id: row.id,
            showDetail: showDetail
          }// props
        },
        area: ['9999px', '9999px'],
        title: showDetail ? '编排工艺' : '预览工艺', // 标题
        shadeClose: false // 点击遮罩是否关闭
      })
    }
  }
}
</script>

<style lang="less" scoped>
</style>
